{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n设置使用cpu运算,而非GPU\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_DEVICE_ORDER\"] = \"PCI_BUS_ID\"  \n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"-1\"\n",
    "\"\"\"\n",
    "设置使用cpu运算,而非GPU\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "result: [[1.0000000e+00 6.5914426e-16]]\n",
      "result: [[1.000000e+00 9.443143e-15]]\n",
      "result: [[1.0000000e+00 3.6303037e-11]]\n",
      "result: [[1.00000e+00 9.32857e-15]]\n",
      "result: [[1.0000000e+00 2.5016649e-14]]\n",
      "result: [[1.0000000e+00 5.0525285e-14]]\n",
      "result: [[0.11433201 0.885668  ]]\n",
      "result: [[0.01752824 0.98247176]]\n",
      "result: [[1.0000000e+00 4.3565988e-14]]\n",
      "result: [[1.0000000e+00 2.1203107e-14]]\n",
      "result: [[1.0000000e+00 2.4464793e-14]]\n",
      "result: [[1.000000e+00 5.811461e-12]]\n",
      "result: [[1.0000000e+00 2.0558168e-10]]\n",
      "result: [[1.0000000e+00 7.1226264e-10]]\n",
      "result: [[1.0000000e+00 2.6423743e-11]]\n",
      "result: [[1.000000e+00 8.271292e-12]]\n",
      "result: [[1.0000000e+00 2.1902627e-11]]\n",
      "result: [[1.0000000e+00 2.2605835e-10]]\n",
      "result: [[1.0000000e+00 7.8444404e-11]]\n",
      "result: [[1.000000e+00 5.902671e-13]]\n",
      "result: [[1.0000000e+00 2.3429023e-12]]\n",
      "result: [[1.0000000e+00 4.4164157e-08]]\n",
      "result: [[1.0000000e+00 1.2380792e-13]]\n",
      "result: [[1.000000e+00 7.050586e-13]]\n",
      "result: [[1.0000000e+00 5.7350007e-11]]\n",
      "result: [[9.9999964e-01 3.1540438e-07]]\n",
      "result: [[1.0000000e+00 8.7626635e-11]]\n",
      "result: [[1.000000e+00 7.637033e-10]]\n",
      "result: [[1.000000e+00 5.321636e-09]]\n",
      "result: [[9.9999666e-01 3.3707649e-06]]\n",
      "result: [[9.9999797e-01 2.0195137e-06]]\n",
      "result: [[9.9997306e-01 2.6990658e-05]]\n",
      "result: [[9.9999917e-01 7.9700868e-07]]\n",
      "result: [[9.999969e-01 3.119694e-06]]\n",
      "result: [[9.9997377e-01 2.6267408e-05]]\n",
      "result: [[1.0000000e+00 4.2747743e-09]]\n",
      "result: [[1.0000000e+00 2.7112066e-09]]\n",
      "result: [[1.6844598e-10 1.0000000e+00]]\n",
      "result: [[1.2544468e-10 1.0000000e+00]]\n",
      "result: [[3.9695333e-08 1.0000000e+00]]\n",
      "result: [[1.7404068e-08 1.0000000e+00]]\n",
      "result: [[4.828952e-10 1.000000e+00]]\n",
      "result: [[6.2840777e-10 1.0000000e+00]]\n",
      "result: [[9.224491e-09 1.000000e+00]]\n",
      "result: [[1.3891442e-07 9.9999988e-01]]\n",
      "result: [[8.50984e-09 1.00000e+00]]\n",
      "result: [[6.750226e-08 9.999999e-01]]\n",
      "result: [[2.5508805e-07 9.9999976e-01]]\n",
      "result: [[1.9389944e-07 9.9999976e-01]]\n",
      "result: [[6.0880126e-08 9.9999988e-01]]\n",
      "result: [[9.3356482e-07 9.9999905e-01]]\n",
      "result: [[2.1247142e-07 9.9999976e-01]]\n",
      "result: [[1.9172745e-07 9.9999976e-01]]\n",
      "result: [[8.416414e-08 9.999999e-01]]\n",
      "result: [[2.7787979e-07 9.9999976e-01]]\n",
      "result: [[2.9414966e-07 9.9999976e-01]]\n",
      "result: [[4.4782743e-07 9.9999952e-01]]\n",
      "result: [[9.090736e-08 9.999999e-01]]\n",
      "result: [[9.761516e-08 9.999999e-01]]\n",
      "result: [[7.985465e-08 9.999999e-01]]\n",
      "result: [[3.1727236e-08 1.0000000e+00]]\n",
      "result: [[3.8237158e-07 9.9999964e-01]]\n",
      "result: [[5.446009e-07 9.999994e-01]]\n",
      "result: [[4.6604137e-07 9.9999952e-01]]\n",
      "result: [[2.5635595e-07 9.9999976e-01]]\n",
      "result: [[2.4548382e-07 9.9999976e-01]]\n",
      "result: [[1.9606817e-07 9.9999976e-01]]\n",
      "result: [[1.2026099e-07 9.9999988e-01]]\n",
      "result: [[6.174303e-08 9.999999e-01]]\n",
      "result: [[3.27566e-08 1.00000e+00]]\n",
      "result: [[2.8405421e-07 9.9999976e-01]]\n",
      "result: [[1.0457209e-07 9.9999988e-01]]\n",
      "result: [[1.6817136e-08 1.0000000e+00]]\n",
      "result: [[2.3426217e-07 9.9999976e-01]]\n",
      "result: [[5.725763e-08 1.000000e+00]]\n",
      "result: [[5.5917974e-07 9.9999940e-01]]\n",
      "result: [[1.0868116e-07 9.9999988e-01]]\n",
      "result: [[7.1024736e-08 9.9999988e-01]]\n",
      "result: [[1.5018081e-07 9.9999988e-01]]\n",
      "result: [[2.8794897e-08 1.0000000e+00]]\n",
      "result: [[1.0463773e-07 9.9999988e-01]]\n",
      "result: [[2.2169493e-07 9.9999976e-01]]\n",
      "result: [[1.3474914e-07 9.9999988e-01]]\n",
      "result: [[1.2379666e-08 1.0000000e+00]]\n",
      "result: [[7.886533e-08 9.999999e-01]]\n",
      "result: [[1.2877064e-08 1.0000000e+00]]\n",
      "result: [[1.8113683e-10 1.0000000e+00]]\n",
      "result: [[1.3177218e-10 1.0000000e+00]]\n",
      "result: [[8.781637e-11 1.000000e+00]]\n",
      "result: [[4.85494e-10 1.00000e+00]]\n",
      "result: [[4.015825e-09 1.000000e+00]]\n",
      "result: [[1.0000000e+00 2.1407886e-13]]\n",
      "result: [[1.000000e+00 4.243698e-14]]\n",
      "result: [[1.0000000e+00 3.8781608e-13]]\n",
      "result: [[1.0000000e+00 4.6010605e-08]]\n",
      "result: [[9.9999928e-01 6.6041656e-07]]\n",
      "result: [[9.9999964e-01 3.3792253e-07]]\n",
      "result: [[9.9999976e-01 2.3660463e-07]]\n",
      "result: [[1.0000000e+00 3.9287606e-08]]\n",
      "result: [[9.99933e-01 6.69759e-05]]\n",
      "result: [[1.0000000e+00 1.0440643e-08]]\n",
      "result: [[1.0000000e+00 4.2045565e-11]]\n",
      "result: [[1.0000000e+00 3.5135646e-09]]\n",
      "result: [[1.0000000e+00 4.1111788e-11]]\n",
      "result: [[1.0000000e+00 1.7770695e-09]]\n",
      "result: [[9.9999976e-01 2.6678063e-07]]\n",
      "result: [[1.0000000e+00 5.0434164e-08]]\n",
      "result: [[9.9999976e-01 2.8874680e-07]]\n",
      "result: [[9.9998856e-01 1.1438128e-05]]\n",
      "result: [[0.99871576 0.00128432]]\n",
      "result: [[9.9999893e-01 1.0927893e-06]]\n",
      "result: [[1.0000000e+00 4.9554283e-10]]\n",
      "result: [[1.0000000e+00 1.1782122e-10]]\n",
      "result: [[1.000000e+00 4.995983e-09]]\n",
      "result: [[1.0000000e+00 2.0784212e-09]]\n",
      "result: [[1.0000000e+00 7.2588597e-09]]\n",
      "result: [[1.0000000e+00 1.8327017e-11]]\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "__author__ = 'Seven'\n",
    " \n",
    "import cv2\n",
    "from face_train import Model\n",
    " \n",
    "if __name__ == '__main__':\n",
    "    # 加载模型\n",
    "    model = Model()\n",
    "    model.load_model(file_path='./mode_h5/meAndLimodel.h5')\n",
    " \n",
    "    # 框住人脸的矩形边框颜色\n",
    "    color = (0, 255, 0)\n",
    " \n",
    "    # 捕获指定摄像头的实时视频流\n",
    "    camera = cv2.VideoCapture(0)\n",
    " \n",
    "    # 人脸识别分类器本地存储路径\n",
    "    cascade_path = r\"C:\\Program Files\\Polyspace\\R2019b\\mcr\\toolbox\\vision\\visionutilities\\classifierdata\\cascade\\haar\\haarcascade_frontalface_alt2.xml\"\n",
    "     \n",
    "    # 循环检测识别人脸\n",
    "    while True:\n",
    "        ret, frame = camera.read()  # 读取一帧视频\n",
    " \n",
    "        # 图像灰化，降低计算复杂度\n",
    "        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
    " \n",
    "        # 使用人脸识别分类器，读入分类器\n",
    "        cascade = cv2.CascadeClassifier(cascade_path)\n",
    " \n",
    "        # 利用分类器识别出哪个区域为人脸\n",
    "        faces = cascade.detectMultiScale(gray, 1.1, 5)\n",
    "        if len(faces) > 0:\n",
    "            for (x, y, w, h) in faces:\n",
    "                # 截取脸部图像提交给模型识别这是谁\n",
    "                image = frame[y: y + h, x: x + w]\n",
    "                faceID = model.face_predict(image)\n",
    " \n",
    "                # 如果是“我”\n",
    "                if faceID == 0:\n",
    "                    cv2.rectangle(frame, (x, y), (x + w, y + h), color, thickness=2)\n",
    " \n",
    "                    # 文字提示是谁\n",
    "                    cv2.putText(frame, 'me',\n",
    "                                (x + 30, y + 30),  # 坐标\n",
    "                                cv2.FONT_HERSHEY_SIMPLEX,  # 字体\n",
    "                                1,  # 字号\n",
    "                                (255, 0, 255),  # 颜色\n",
    "                                2)  # 字的线宽\n",
    "                else:\n",
    "                    cv2.rectangle(frame, (x, y), (x + w, y + h), color, thickness=2)\n",
    " \n",
    "                    # 文字提示是谁\n",
    "                    cv2.putText(frame, 'LiYuanJun',\n",
    "                                (x + 30, y + 30),  # 坐标\n",
    "                                cv2.FONT_HERSHEY_SIMPLEX,  # 字体\n",
    "                                1,  # 字号\n",
    "                                (255, 0, 255),  # 颜色\n",
    "                                2)\n",
    " \n",
    "        cv2.imshow(\"camera\", frame)\n",
    " \n",
    "        # 等待1毫秒看是否有按键输入\n",
    "        k = cv2.waitKey(1)\n",
    "        # 如果输入q则退出循环\n",
    "        if k & 0xFF == ord('q'):\n",
    "            break\n",
    " \n",
    "    # 释放摄像头并销毁所有窗口\n",
    "    camera.release()\n",
    "    cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
